using Godot;
using System;
using System.Threading.Tasks;

public partial class Main : Node2D
{
	private Map _map;
	private Player _player;
	private CInventory _c_inventory;
	private SaveLoadManager _saveLoadManager;
	
	
	// Called when the node enters the scene tree for the first time.
	public override void _Ready()
	{
		change_scene();
		var Game = GetNode<Game>("/root/Game");
		_map = Game.get_map();
		_player = this.GetNode<Player>("%Player");
		_player.Initialize();
		//_player._Ready();
		_c_inventory = _player.GetNode<CInventory>("%CInventory");
		//Item item = GD.Load<Item>("res://resources/items/key.tres");
		//Item IDcard = GD.Load<Item>("res://resources/items/IDcard.tres");
		//_c_inventory.add_item(item);
		//_c_inventory.add_item(IDcard);
		_saveLoadManager = GetNode<SaveLoadManager>("%SaveLoadManager");
		var UIControl = GetNode<UIControl>("/root/UIControl");
		UIControl.GetGuiViewManager().open_view("UiStart");
	}
	public void change_scene()
	{
		var childNodes = this.GetChildren();

		foreach (var child in childNodes)
		{
			if (child is Map)
			{
				_map = (Map)child;
			}
			
		}
	}

	// Called every frame. 'delta' is the elapsed time since the previous frame.
	public override void _Process(double delta)
	{
		var Game = GetNode<Game>("/root/Game");
		_player.Update(delta);
		change_scene();
		var UIControl = GetNode<UIControl>("/root/UIControl");
		
		if(Input.IsActionJustReleased("esc")&&UIControl.GetGuiViewManager().InstanceUIsList.Count == 0&&Game._timer.IsStopped())
		{
			UIControl.GetGuiViewManager().open_view("UiSystem");
			GD.Print("anesc");
			
			Game._timer.Start(0.1);
		}
		if(Input.IsActionJustReleased("bag")&&UIControl.GetGuiViewManager().InstanceUIsList.Count == 0&&Game._timer.IsStopped())
		{
			UIControl.GetGuiViewManager().open_view("UiInventory");
			Game._timer.Start(0.1);
		}
		if(Input.IsActionJustPressed("tab"))
		{
			UIControl.GetGuiViewManager().open_view("UiHandChooseBlock");
			//GD.Print("打开轮盘");
		}
		if(Input.IsActionJustReleased("q")&&UIControl.GetGuiViewManager().InstanceUIsList.Count == 0&&Game._timer.IsStopped())
		{
			UIControl.GetGuiViewManager().open_view("UiDialog");
			Game._timer.Start(0.1);
		}
		if(Input.IsActionJustReleased("j")&&UIControl.GetGuiViewManager().InstanceUIsList.Count == 0&&Game._timer.IsStopped())
		{
			UIControl.GetGuiViewManager().open_view("UiTask");
			Game._timer.Start(0.1);
		}

		_map._Process(delta);
		
	}


	public void InputCheck()
	{}
}
